package com.wengyingjian.https;

import okhttp3.*;

import java.io.IOException;
import java.util.Map;

/**
 * Created by hzwengyingjian on 17/1/1.
 */
public class HttpUtils {
    private static final OkHttpClient client = new OkHttpClient();

    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    public static String get(String url, String cookie) throws IOException {
        Request request = new Request.Builder()//
                .url(url)//
                .addHeader("Cookie", cookie)//
                .build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("HttpUtil get failed: Unexpected code " + response);
        return response.body().string();
    }

    public static String get(String url) throws IOException {
        Request request = new Request.Builder()//
                .url(url)//
                .build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("HttpUtil get failed: Unexpected code " + response);
        return response.body().string();
    }

    public static String post(String url, String json, Map<String, String> cookies) throws IOException {
        RequestBody requestBody = RequestBody.create(JSON, json);
        Request request = new Request.Builder()//
                .url(url)//
                .addHeader("Cookie", getCookieStr(cookies))//
                .method("POST", requestBody)//
                .build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("HttpUtil post failed: Unexpected code " + response);
        return response.body().string();
    }

    public static String post(String url, Map<String, String> form, Map<String, String> cookies) throws IOException {
        FormBody.Builder bodyBuilder = new FormBody.Builder();
        for (Map.Entry<String, String> entry : form.entrySet()) {
            bodyBuilder.add(entry.getKey(), entry.getValue());
        }

        Request request = new Request.Builder()//
                .url(url)//
                .addHeader("Cookie", getCookieStr(cookies))//
                .method("POST", bodyBuilder.build())//
                .build();
        Response response = client.newCall(request).execute();
        if (!response.isSuccessful())
            throw new IOException("HttpUtil post failed: Unexpected code " + response);
        return response.body().string();
    }

    private static String getCookieStr(Map<String, String> cookies) {
        String cookieStr = "";
        if (cookies == null) {
            return cookieStr;
        }
        for (Map.Entry<String, String> cookie : cookies.entrySet()) {
            cookieStr += cookie.getKey() + "=" + cookie.getValue() + "; ";
        }
        return cookieStr;
    }

    public static void main(String[] args) throws IOException {
    }
}
